﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Web;

namespace LY.Common.Utils
{
   public class WebConfig
    {
       /// <summary>
        /// 获取AppSetting里面的内容
       /// </summary>
       /// <typeparam name="T"></typeparam>
       /// <param name="defaultValue"></param>
       /// <param name="key"></param>
       /// <returns></returns>
        public static T GetAppSettingValue<T>(T defaultValue, string key)
        {
            string value = ConfigurationManager.AppSettings[key];
            if (!string.IsNullOrEmpty(value))
            {
                try
                {
                    defaultValue = (T)Convert.ChangeType(value, typeof(T));
                }
                catch
                {
                }
            }
            return defaultValue;
        }
       /// <summary>
        /// 获取指定的Config文件的AppSetting里面的指定键值对应的value值
       /// </summary>
       /// <typeparam name="T"></typeparam>
       /// <param name="defaultValue"></param>
       /// <param name="key"></param>
       /// <param name="file"></param>
       /// <returns></returns>
       public static T GetAppSettingValue<T>(T defaultValue, string key, string file)
         {
             var map = new ExeConfigurationFileMap
             {
                 ExeConfigFilename = file
             };
             Configuration config = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
             string value = config.AppSettings.Settings[key].Value;
 
             if (!string.IsNullOrEmpty(value))
             {
                 try
                 {
                     defaultValue = (T)Convert.ChangeType(value, typeof(T));
                 }
                 catch
                 {
                 }
             }
             return defaultValue;
         }

     
    }
}
